home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12313 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  128 lines

  1. Newsgroups: comp.lang.c++
  2. Path: lion.cs.latrobe.edu.au!boylesgj
  3. From: boylesgj@lion.cs.latrobe.edu.au (Gregary J Boyles)
  4. Subject: Operator overload problem!
  5. X-Nntp-Posting-Host: lion.cs.latrobe.edu.au
  6. Message-ID: <DoIn1z.Gou@latcs1.lat.oz.au>
  7. Sender: news@latcs1.lat.oz.au (news)
  8. Organization: Comp.Sci & Comp.Eng, La Trobe Uni, Australia
  9. Date: Tue, 19 Mar 1996 13:05:58 GMT
  10.  
  11. I am trying to overload the << but I get the syntax errors : friends must be functions or
  12. classes, 'ostream' cannot start a parameter declaration, 'Address::operator <<(int &,Address &)'
  13. must be declared at the first set of astericks and declaration syntax error at the second set.
  14.  
  15. What the #$%& is it on about? I copied the operator overload function directly out of a program
  16. which compiles and runs so why all of a sudden won't the bloody compiler accept it?
  17.  
  18.  
  19. #include "defines.h"
  20.  
  21. class Address
  22. {
  23.      private : int Number;
  24.            char *Street;
  25.            char *City;
  26.            int Zip;
  27.  
  28.      public : // Constructors
  29.           Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
  30.           Address();
  31.           // Copy constructor
  32.           Address(Address& AnAddress);
  33.           // Deconstructor
  34.           ~Address();
  35.           // Operator overloads
  36.  
  37.         **************************************************************************
  38.         *                                             *
  39.         * friend ostream& operator <<(ostream& OutPutStream,Address& AnAddress); *
  40.         *                                             *
  41.         **************************************************************************
  42.  
  43.           // Functions
  44.           void Change(int ANumber,const char *AStreet,const char *ACity,int AZip);
  45. };
  46.  
  47. #endif
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. // address.cpp
  56.  
  57. #include "address.h"
  58. #include <string.h>
  59.  
  60. // Constructors
  61. Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
  62. {
  63.      Number=ANumber;
  64.      Street=new char[strlen(AStreet)+1];
  65.      strcpy(Street,AStreet);
  66.      City=new char[strlen(ACity)+1];
  67.      strcpy(City,ACity);
  68.      Zip=AZip;
  69. }
  70.  
  71. Address::Address()
  72. {
  73.      Number=0;
  74.      Street=new char[strlen("")+1];
  75.      strcpy(Street,"");
  76.      City=new char[strlen("")+1];
  77.      strcpy(City,"");
  78.      Zip=0;
  79. }
  80.  
  81. // Copy constructor
  82. Address::Address(Address& AnAddress)
  83. {
  84.      Number=AnAddress.Number;
  85.      Street=new char[strlen(AnAddress.Street)+1];
  86.      strcpy(Street,AnAddress.Street);
  87.      City=new char[strlen(AnAddress.City)+1];
  88.      strcpy(City,AnAddress.City);
  89.      Zip=AnAddress.Zip;
  90. }
  91.  
  92. // Deconstructor
  93. Address::~Address()
  94. {
  95.      Number=0;
  96.      strcpy(Street,"");
  97.      strcpy(City,"");
  98.      Zip=0;
  99. }
  100.  
  101. // Operator overloads
  102.  
  103. ***************************************************************
  104.                                   *
  105. ostream& operator <<(ostream& OutPutStream,Address& AnAddress)*
  106.                                   *
  107. ***************************************************************
  108.  
  109. {
  110.      OutPutStream<<"Street : "<<AnAddress.Number<<" "<<AnAddress.Street<<EOLN;
  111.      OutPutStream<<"City : "<<AnAddress.City<<EOLN;
  112.      OutPutStream<<"Zip code : "<<AnAddress.Zip<<EOLN;
  113. }
  114.  
  115. // Functions
  116. void Address::Change(int ANumber,const char *AStreet,const char *ACity,int AZip)
  117. {
  118.      Number=ANumber;
  119.      delete Street;
  120.      Street=new char[strlen(AStreet)+1];
  121.      strcpy(Street,AStreet);
  122.      delete City;
  123.      City=new char[strlen(ACity)+1];
  124.      strcpy(City,ACity);
  125.      Zip=AZip;
  126. }
  127.  
  128.